<template>
    <div>
         <div class="formContain">
            <titlebar :title-left='titleLeft1'></titlebar>
            <ul>
               <!-- <li class="flex" style="position: relative">
                  <span class="label"><i class="require" v-if="isFirstTask == '00901'">*</i>申请人</span>
                  <div v-if="isFirstTask == '00900'">
                    <span>{{datas.applyUserName}}</span>
                  </div>
                  <div v-if="isFirstTask == '00901'">
                    <input type="text" style="width: 10rem"  id="applyPerson" placeholder="请选择申请人" @input="searchApply">
                  </div>
                  <ul v-show="applysearch" class="search">
                    <li @click="applychose" :id="item.id" v-for="item in applyList">
                      {{item.orgName}}-{{item.postName}}-{{item.name}}
                    </li>
                  </ul>
                </li>-->
                <li class="flex" style="position: relative">
                  <span class="label"><i class="require" v-if="isFirstTask == '00901'">*</i>被调班人员</span>
                  <div v-if="isFirstTask == '00900'">
                    <span>{{datas.chanedUserName}}</span>
                  </div>
                  <div v-if="isFirstTask == '00901'">
                    <input type="text" style="width: 10rem"  id="checkPerson" placeholder="请选择被调人员" @input="searchBPerson">
                  </div>
                  <ul v-show="search" class="search">
                    <li @click="chose" :id="item.id" v-for="item in personList">
                      {{item.orgName}}-{{item.postName}}-{{item.name}}
                    </li>
                  </ul>
                </li>
                <li class="flex">
                    <span class="label"><i class="require" v-if="isFirstTask == '00901'">*</i>原定班次开始时间</span>
                    <div v-if="isFirstTask == '00900'">
                        <span>{{datas.startTimeOld}}</span>
                    </div>
                    <div @click="showdatepicker($event,'startTimeOld')" v-if="isFirstTask == '00901'">
                      <input type="text"  id="beginold" placeholder="请选择时间" :value="datas.startTimeOld"  readonly disabled>
                      <img src="@/assets/images/img/calendarIcon.png" alt="" srcset="" >
                    </div>
                </li>
                 <li class="flex">
                    <span class="label"><i class="require" v-if="isFirstTask == '00901'">*</i>原定班次结束时间</span>
                    <div v-if="isFirstTask == '00900'">
                        <span>{{datas.endTimeOld}}</span>
                    </div>
                     <div @click="showdatepicker($event,'endTimeOld')" v-if="isFirstTask == '00901'">
                       <input type="text"  id="endold" placeholder="请选择时间" :value="datas.endTimeOld"  readonly disabled>
                       <img src="@/assets/images/img/calendarIcon.png" alt="" srcset="" >
                     </div>
                </li>
                  <li class="flex">
                      <span class="label"><i class="require" v-if="isFirstTask == '00901'"></i>原班次</span>
                    <div v-if="isFirstTask == '00900'">
                        <span>{{datas.classOldName}}</span>
                    </div>
                    <div v-if="isFirstTask == '00901'">
                      <van-field v-model="datas.classOldName" disabled placeholder="请输入原班次" type="text" input-align="right"/>
                    </div>
                </li>
                 <li class="flex">
                      <span class="label"><i class="require" v-if="isFirstTask == '00901'">*</i>共计调整天数</span>
                    <div v-if="isFirstTask == '00900'">
                       <span>{{datas.changeDaysOld}}</span>
                    </div>
                     <div v-if="isFirstTask == '00901'">
                       <van-field v-model="datas.changeDaysOld" maxlength="6" @input="checkNum" placeholder="请输入总天数" type="number" input-align="right"/>
                     </div>
                </li>
                 <li class="flex">
                    <span class="label"><i class="require" v-if="isFirstTask == '00901'">*</i>调整班次开始日期</span>
                    <div v-if="isFirstTask == '00900'">
                        <span>{{datas.startTimeChanaged}}</span>
                    </div>
                     <div @click="showdatepicker($event,'startTimeChanaged')" v-if="isFirstTask == '00901'">
                       <input type="text"  id="beginchange" placeholder="请选择时间" :value="datas.startTimeChanaged"  readonly disabled>
                       <img src="@/assets/images/img/calendarIcon.png" alt="" srcset="" >
                     </div>
                </li>
                 <li class="flex">
                    <span class="label"><i class="require" v-if="isFirstTask == '00901'">*</i>调整班次结束日期</span>
                    <div v-if="isFirstTask == '00900'">
                        <span>{{datas.endTimeChanaged}}</span>
                    </div>
                     <div @click="showdatepicker($event,'endTimeChanaged')" v-if="isFirstTask == '00901'">
                       <input type="text"  id="endchange" placeholder="请选择时间" :value="datas.endTimeChanaged"  readonly disabled>
                       <img src="@/assets/images/img/calendarIcon.png" alt="" srcset="" >
                     </div>
                </li>
                  <li class="flex">
                      <span class="label"><i class="require" v-if="isFirstTask == '00901'"></i>调整班次</span>
                    <div v-if="isFirstTask == '00900'">
                       <span>{{datas.classChanagedName}}</span>
                    </div>
                    <div v-if="isFirstTask == '00901'">
                      <van-field v-model="datas.classChanagedName" disabled placeholder="请输入调整班次" type="text" input-align="right"/>
                    </div>
                </li>
                 <li class="flex">
                      <span class="label"><i class="require" v-if="isFirstTask == '00901'">*</i>共计调整天数</span>
                    <div v-if="isFirstTask == '00900'">
                        <span>{{datas.changeDaysChanaged}}</span>
                    </div>
                     <div v-if="isFirstTask == '00901'">
                       <van-field v-model="datas.changeDaysChanaged" maxlength="6" @input="checkNum1" placeholder="请输入总天数" type="number" input-align="right"/>
                     </div>
                </li>
            </ul>

        </div>
         <div class="formContain">
            <titlebar :title-left='titleLeft3'></titlebar>
            <ul>
                <li class="flex">
                    <div class='full_width' style="min-height:2rem" v-if="isFirstTask == '00900'">
                        <van-field class='full_width' :value="datas.remark" rows="1" autosize readonly="readonly" type="textarea" input-align="left"/>
                    </div>
                    <div class='full_width' style="min-height:2rem" v-if="isFirstTask == '00901'">
                      <i class="require">*</i>
                      <van-field class='full_width'  rows="1" autosize v-model="datas.remark" placeholder="请输入调班原因" type="textarea" input-align="left"/>
                    </div>
                </li>
              <li class="flex" v-if="isFirstTask == '00900'">
                <span class="label">预览附件</span>
                <div style="flex-grow: 1">
                  <!-- <input class="file" type="file"  id="type"  readonly> -->
                  <input type="hidden" v-model="datas.attachmentId">
                  <input type="text" style="width: 10rem;background: #fff" v-model="fileName" disabled>
                  <span class="iconfont icon-yulan" style="font-size: 1rem" @click="downFile"></span>
                </div>
              </li>
              <li class="flex"  v-if="isFirstTask == '00901'">
                <span class="label">上传附件</span>
                <div>
                  <!-- <input class="file" type="file"  id="file"  readonly>-->
                  <input type="text" v-model="fileName" disabled style="background: transparent;width: 10rem;position: absolute;padding-right: 1rem">
                  <van-uploader :after-read="afterRead" :before-read="beforeRead" id="file">
                    <span class="iconfont icon-hao"></span>
                  </van-uploader>
                </div>
              </li>
              <van-image-preview
                v-model="show1"
                :images="images"
              >
              </van-image-preview>
            </ul>
         </div>
      <van-picker id="picker"   show-toolbar  :title="title" v-show='pickershow' :columns="columns" @cancel="onCancel" @confirm="onConfirm" />
      <van-datetime-picker v-show='show' id="datetime-picker" type="datetime" @confirm="datetimeSelect" @cancel='cancel' v-model="currentTime" />
    </div>
</template>

<script>
    import headTop from '@/components/header/head'
    import titlebar from '@/components/titlebar3/titlebar3';
    import {actdata,uploadFile,downloadFile,downloadFileType,searchPerson,getWorkClassInfo} from '@/service/getData';
    import {getStore,getDefault} from '@/assets/config/mUtils';
    import { Dialog,Toast} from 'vant';
    export default {
    	data(){
            return{
                profiletitle:'调班',
                goBack:true,
                titleLeft1:'个人调班信息',
                titleLeft2:'与他人调班信息',
                titleLeft3:'调班事由',
                show:false, //控制日期显示隐藏
                pickershow:false,//控制下拉选显示隐藏
                pickerName:'',//当前控制日期的控件
                currentTime:new Date(),
                search:false,
                applysearch:false,
                fileName:'',
                screenHeight:'',
                images:[],
                show1: false,
                personList:[],
                applyList:[],
                datas:{
                  applyUser:"",
                  applyUserName:"",
                  chanedUserName:"",
                  chanedUser:"",
                  startTimeOld:"",
                  endTimeOld:"",
                  classOld:"",
                  classOldName:'',
                  changeDaysOld:"",
                  startTimeChanaged:"",
                  endTimeChanaged:"",
                  classChanaged:"",
                  classChanagedName:'',
                  changeDaysChanaged:"",
                  remark:"",
                  attachmentId:""
                },
                title:'标题',//下拉框标题
                columns: ['杭州', '宁波', '温州', '嘉兴', '湖州']  //下拉框数据
            }
        },
        props:['isFirstTask','procInstanceId'],
        mounted(){
          this.screenHeight = document.documentElement.clientHeight;
          this.$nextTick(function (){
            var actData = {};
            actData.procInstanceId = this.procInstanceId;
            actData.app_userId = getStore('user_id');
            actData.CHECK_TIME = new Date().getTime();
            actData.CHECK_SIGN = this.$md5(actData.app_userId + actData.CHECK_TIME +"3a28fba00c015e0df7f88556c91cb7fc");
            actdata(actData).then(res=>{
              var resData = res.data.datas[0];
              if(this.isFirstTask == '00901'){
                /*document.getElementById("applyPerson").value = resData.applyUserName;*/
                document.getElementById("checkPerson").value = resData.chanedUserName;
              }
              console.log(resData,"调班数据");
              this.datas = resData;
              var data = {};
              data.fileId = this.datas.attachmentId||"";
              //data.fileId = '08ed9a58d7184c1898def154c05be2ec';
              downloadFileType(data).then(res=>{
                this.fileName = res.data;
              })
            })
          })
        },
        updated () {
        let that = this;
        this.$nextTick(function () {
          let $inputs = Array.from(document.getElementsByTagName('input'));
          /*let body = document.body;
          $inputs.forEach(item => { // 定时器是处理多个input间快速切换闪屏问题，可省略
              item.onblur = function () { // onblur是核心方法
                  clearTimeout(body.timer);
                  body.timer = setTimeout(() => {
                  window.scroll(0, 0);
                  window.innerHeight = window.outerHeight = that.screenHeight
                  }, 150)
              }
              item.onfocus = function () {
                  clearTimeout(body.timer)
              }
          })*/
          let body = document.body;
          $inputs.forEach(item => {
            item.onblur = function () { // onblur是核心方法
              window.scroll(0, 0);
              window.innerHeight = window.outerHeight = that.screenHeight;
              //console.log(that.screenHeight);
            }
          })
        })
      },
        components:{
            headTop,
            titlebar
        },
        watch:{
          datas:{
            handler: function (newVal,oldVal) {
              this.$emit('childactdata',this.datas)
            },
            deep: true
          }
        },
        methods:{
          checkNum(){
            this.datas.changeDaysOld = parseFloat(this.datas.changeDaysOld);
          },
          checkNum1(){
            this.datas.changeDaysChanaged = parseFloat(this.datas.changeDaysChanaged);
          },
            addZero(num){
              var addNum = num<10?"0"+num:num;
              return addNum;
            },
            datetimeSelect(value){
              this.show = false;
              var date = value;
              var val = date.getFullYear() + '-'+this.addZero(date.getMonth()+1) + '-'+ this.addZero(date.getDate())+' '+this.addZero(date.getHours()) + ':'+ this.addZero(date.getMinutes())+':'+"00";
              this.$set(this.datas,this.pickerName,val);
              // document.getElementById(this.pickerName).value = val;
              if(this.datas.endTimeOld != ""){
                if(this.datas.startTimeOld >= this.datas.endTimeOld){
                  Dialog.alert({
                    title: '',
                    message: '开始时间不能大于结束时间'
                  });
                  this.datas.endTimeOld = '';
                }else {
                  var data = {};
                  data.app_userId = getStore('user_id');
                  data.personId = getStore('user_id');
                  data.atndStartDate = this.datas.startTimeOld;
                  data.atndEndDate = this.datas.endTimeOld;
                  data.CHECK_TIME = new Date().getTime().toString();
                  data.CHECK_SIGN = this.$md5(data.app_userId + data.CHECK_TIME +"3a28fba00c015e0df7f88556c91cb7fc");
                  getWorkClassInfo(data).then(res=>{
                    console.log(res,"调班信息");
                    if(res.data.result_value == ""){
                      Dialog.alert({
                        title: '提示',
                        message: '暂无排班信息'
                      });
                    }else {
                      this.datas.classOld = res.data.result_value;
                      this.datas.classOldName =res.data.result_text;
                    }

                  })
                }
              };

              /*调班时间以及调班班次查询*/
              if(this.datas.endTimeChanaged != ""){
                if(this.datas.startTimeChanaged >= this.datas.endTimeChanaged){
                  Dialog.alert({
                    title: '',
                    message: '开始时间不能大于结束时间'
                  });
                  this.datas.endTimeChanaged = '';
                }else {
                  if(this.datas.chanedUser == ''){
                    Dialog.alert({
                      title: '提示',
                      message: '请先选择被调班人员'
                    });
                  }else {
                    var data = {};
                    data.app_userId = getStore('user_id');
                    data.personId = this.datas.chanedUser;
                    data.atndStartDate = this.datas.startTimeChanaged;
                    data.atndEndDate = this.datas.endTimeChanaged;
                    data.CHECK_TIME = new Date().getTime().toString();
                    data.CHECK_SIGN = this.$md5(data.app_userId + data.CHECK_TIME +"3a28fba00c015e0df7f88556c91cb7fc");
                    getWorkClassInfo(data).then(res=>{
                      console.log(res,"调班信息");
                      if(res.data.result_value == ""){
                        Dialog.alert({
                          title: '提示',
                          message: '暂无排班信息'
                        });
                      }else {
                        this.datas.classChanaged = res.data.result_value;
                        this.datas.classChanagedName = res.data.result_text;
                      }

                    })
                  }

                }
              };
            },
            onConfirm(value){
                this.$set(this.datas,this.pickerName,value);
                 this.pickershow = false;
            },
            cancel() {
                this.show = false;
            },
            onCancel(){
                this.pickershow = false;
            },
            showdatepicker(event,name){
                this.show = true;
                this.pickerName = name;
            },
            showpicker(event,name,title){
                this.pickershow = true;
                this.pickerName = name;
                this.title = title;
            },
          beforeRead(file) {
            var imgType = file.type.toLocaleLowerCase();
            if (imgType != 'image/jpg'&&imgType != 'image/jpeg'&&imgType != 'image/png'&&imgType != 'image/gif'&&imgType != 'image/bmp') {
              Toast('请上传图片');
              return false;
            }

            return true;
          },
          afterRead(file) {
            // 此时可以自行将文件上传至服务器
            Toast.loading({
              duration: 0,   // 持续展示 toast
              mask: true,
              message: '正在上传...'
            });
            var formData = new FormData();
            formData.append("uploadFile", file.file);
            uploadFile(formData).then(res=>{
              this.fileName = file.file.name;
              this.datas.attachmentId = res.fileId;
              Toast.clear();
            })
          },
          downFile(){
            var data = {};
            //96c4cf9754b7492b91b53a8a44a5d74b    08ed9a58d7184c1898def154c05be2ec
            data.fileId = this.datas.attachmentId||"";
            data.app_userId = getStore('user_id');
            data.CHECK_TIME = new Date().getTime().toString();
            data.CHECK_SIGN = this.$md5(data.app_userId + data.CHECK_TIME +"3a28fba00c015e0df7f88556c91cb7fc");
            //判断有无附件
            downloadFileType(data).then(res=>{
              var fileType = res.data.split(".")[(res.data.split(".").length-1)];
              //console.log(fileType== "",res.code);
              if(fileType == "jpg" ||fileType == "png"||fileType == "jpeg" ||fileType == "gif"||fileType == "bmp"&&res.code =="200"){
                downloadFile(data).then(res=>{
                  //var blob = new Blob(res);
                  if(this.images.length == 0){
                    this.images.push(res.data.imgurl)
                    //this.images.push(window.webkitURL.createObjectURL(res));
                    this.show1 = true;
                  }else {
                    this.show1 = true;
                  }
                });
              }else if(res.code=="400"&& fileType== ""){
                Dialog.alert({
                  title: '提示',
                  message: '暂无附件'
                })
              }else {
                Dialog.alert({
                  title: '提示',
                  message: '文件类型在移动端不支持预览，请在PC端进行查看'
                })
              }
            });

          },
          /*申请人搜索*/
//          searchApply(){
//            var data = {};
//            data.name = document.getElementById("applyPerson").value;
//            data.appUserId = getStore('user_id');
//            data.app_userId = getStore('user_id');
//            data.CHECK_TIME = new Date().getTime().toString();
//            data.CHECK_SIGN = this.$md5(data.app_userId + data.CHECK_TIME +"3a28fba00c015e0df7f88556c91cb7fc");
//            if(data.name!= ''){
//              searchPerson(data).then(res=>{
//                this.applysearch = true;
//                this.applyList = res.data;
//              })
//            }else {
//              this.applyList = [];
//              this.applysearch = false;
//            }
//          },
          /*被申请人搜索*/
          searchBPerson(){
            var data = {};
            data.name = document.getElementById("checkPerson").value;
            data.appUserId = getStore('user_id');
            data.app_userId = getStore('user_id');
            data.CHECK_TIME = new Date().getTime().toString();
            data.CHECK_SIGN = this.$md5(data.app_userId + data.CHECK_TIME +"3a28fba00c015e0df7f88556c91cb7fc");
            if(data.name!= ''){
              searchPerson(data).then(res=>{
                this.search = true;
                this.personList = res.data;
              })
            }else {
              this.personList = [];
              this.search = false;
            }

          },
//          /*申请人选择*/
//          applychose(event){
//            //console.log(event.target.getAttribute("id"));
//            document.getElementById("applyPerson").value = event.target.innerText;
//            this.applysearch = false;
//            this.datas.applyUser = event.target.getAttribute("id");
//            this.datas.applyUserName = event.target.innerText;
//            this.applyList = [];
//          },

          /*被调班人选择*/
          chose(event){
            //console.log(event.target.getAttribute("id"));
            document.getElementById("checkPerson").value = event.target.innerText;
            this.search = false;
            this.datas.chanedUser = event.target.getAttribute("id");
            this.datas.chanedUserName = event.target.innerText;
            this.personList = [];
            var data = {};
            data.app_userId = getStore('user_id');
            data.personId = this.datas.chanedUser;
            data.atndStartDate = this.datas.startTimeChanaged;
            data.atndEndDate = this.datas.endTimeChanaged;
            data.CHECK_TIME = new Date().getTime().toString();
            data.CHECK_SIGN = this.$md5(data.app_userId + data.CHECK_TIME +"3a28fba00c015e0df7f88556c91cb7fc");
            getWorkClassInfo(data).then(res=>{
              if(res.data.result_value == ""){
                Dialog.alert({
                  title: '提示',
                  message: '暂无排班信息'
                });
              }else {
                this.datas.classChanaged = res.data.result_value;
                this.datas.classChanagedName = res.data.result_text;
              }
            })
          },
        }
    }

</script>

<style lang="scss" scoped>
      @import '../../../../assets/style/mixin';
      @import '../../../../assets/style/public';
    .formContain{
        background: #fff;
        >table{
            width: 100%;
            border-collapse: collapse;
            td{
                font-size: .56rem;
                color: #444;
                border-bottom: 1px solid #e0e0e0;
                line-height: 2rem;
                text-align: center;
                width: 33.3%;
            }
        }
        ul{
            padding: 0 .54rem;
            .flex{
                justify-content: space-between;
                align-items: center;
                line-height: 2rem;
                border-bottom: 1px solid #e0e0e0;
                padding: 0 .3rem;
                &:nth-last-of-type(1){
                    border: none;
                }
                .label{
                    font-size: .56rem;
                    line-height: 2rem;
                    color: #333333;
                }
                .opinion{
                    font-size: .56rem;
                    font-weight: normal;
                    font-stretch: normal;
                    line-height: 1.2rem;
                    letter-spacing: 0px;
                    color: #666666;
                    padding-bottom: .3rem;
                }
                div{
                    display: flex;
                    justify-content: flex-end;
                    align-items: center;
                    position: relative;
                    flex-grow: 1;
                    span{
                        font-size: .56rem;
                        color: #333;
                    }
                    img{
                        width: .68rem;
                        height: .64rem;
                    }
                    input{
                        color: #323233;
                        text-align: right;
                        font-size: .48rem;
                        padding-right: .48rem;
                        border: none;
                        background: #fff;
                        &:-ms-input-placeholder{
                             color: #aaa;
                             font-size: .48rem;

                        }
                    }
                    .file{
                        width: 2rem;
                        height: 1rem;
                        font-size: 10rem;
                        opacity: 0;
                        position: absolute;
                        right: 0;
                    }
                    .van-cell{
                        font-size: .48rem;
                        padding-right: .48rem;
                        padding: 0;
                        color: #aaa;
                        .van-field__control{
                            color: #aaa;
                        }
                    }

                }
            }
        }
    }
    .foot-button{
        margin-top: 2rem;
        margin-bottom: .4rem;
        .cancel-btn{
            margin-right: 1rem;
        }
    }
    #datetime-picker,#picker{
        position: fixed;
        width: 100%;
        bottom: 0;
    }
      .search{
        position: absolute;
        width: 100%;
        background: #fff;
        border-top: 1px solid #ccc;
        border-bottom: 1px solid #ccc;
        top: 43px;
        left: -14px;
        z-index: 9;
        max-height: 16rem;
        overflow-y: scroll;
        li{
          border-bottom: 1px solid #ccc;
          font-size: 14px;
          height: 1.5rem;
          line-height: 1.5rem;
          background: url('../../../../assets/images/img/set_person.png') no-repeat 0.1rem;
          background-size: 1rem;
          padding-left: 1.5rem;
        }
      }
      .require{
        color: red;
        margin-right: 4px;
        font-style: inherit;
        font-size: 14px;
      }
</style>
